﻿using Microsoft.Workflow.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Helpers;
using System.Web.Mvc;
using FreeWorkflow;

namespace FreeWorkflow.Administration.Controllers
{
    public class WorkflowController : Controller
    {
        IWorkflowManagerConnector connector = new WorkflowManagerProxy.WorkflowManagerConnector();
        ISettingManager setting = new FreeWorkflow.WorkflowManagerProxy.SettingManager();
        FreeWorkflow.Services.Workflows workflow ;
        public WorkflowController()
        {
            workflow = new Services.Workflows(connector, setting);
        }
        public ActionResult Index()
        {
            //Get List Of Workflow
            var workflows=workflow.GetWorkflows();
            return View(workflows);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Create()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)] 
        public ActionResult Create(String Title,String Xml)
        {
            workflow.PublishWorkflow(Title, Xml);
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult InstantiateWorkflow(String WorkflowTitle)
        {
            String operationResult = workflow.InstantiateWorkflow(WorkflowTitle);
            return Json(new { result = operationResult }, JsonRequestBehavior.AllowGet);
        }
        [HttpGet]
        public ActionResult GetInstances(String WorkflowTitle)
        {
            ICollection<WorkflowInstanceInfo> instances = workflow.GetWorkflowInstances(WorkflowTitle);
            return PartialView("Instances",instances);
        }
	}
}